//
//  SegViewController.m
//  Ting
//
//  Created by 安静的美男子 on 14/11/13.
//  Copyright (c) 2014年 安静的美男子. All rights reserved.
//

#import "SegViewController.h"
#import "SegModel.h"
#import "SegTableViewCell.h"
@interface SegViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)SegModel *model;
@property(nonatomic, retain)UITableView *table;
@property(nonatomic, copy)NSString *type;

@end

@implementation SegViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setHidesBackButton:TRUE animated:NO];
    self.type = @"hot";
    
    [self getData:_type];
    [self creatTable];
    self.mbview = [[MBProgressHUD alloc] initWithView:self.view];
    self.mbview.labelText = @"正在努力加载中...";
    [self.mbview setMode:MBProgressHUDModeIndeterminate];
    [self.mbview show:YES];
    [self.view addSubview:self.mbview];
    [self CreatTabbar];

    // Do any additional setup after loading the view.
}
-(void)creatTable
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height  - 44) style:UITableViewStylePlain];
    _table.delegate = self;
    _table.dataSource = self;
    
    [self.view addSubview:_table];
   }


-(void)getData:(NSString *)type
{
    NSString *str = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_album_list?category_name=%@&condition=%@&device=iPhone&page=1&per_page=20&status=0&tag_name=%@",_url,type,_name];
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    GetData *data = [[GetData alloc]init];
    [data getDataURL:str dic:nil block:^(id result) {
        _model = [[SegModel alloc]init];
        [_model setValuesForKeysWithDictionary:result];
        [_table reloadData];
                [self.mbview removeFromSuperview];
    }];
    
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
    view.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1];
    UISegmentedControl *seg = [[UISegmentedControl alloc]initWithItems:@[@"最火",@"最近更新",@"经典"]];
    if ([_type isEqualToString:@"hot"]) {
        
            seg.selectedSegmentIndex = 0;
    }else if ([_type isEqualToString:@"recent"])
    {
        seg.selectedSegmentIndex = 1;
    }
    else
    {
        seg.selectedSegmentIndex = 2;
    }
    seg.frame = CGRectMake(5, 5, 310, 30);
    seg.tintColor = [UIColor orangeColor];
    [seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    
    
    
    [view addSubview:seg];
    return view;
    
}
-(void)segAction:(UISegmentedControl *)sender
{
    self.mbview = [[MBProgressHUD alloc] initWithView:self.view];
    self.mbview.labelText = @"正在努力加载中...";
    [self.mbview setMode:MBProgressHUDModeIndeterminate];
    [self.mbview show:YES];
    [self.view addSubview:_mbview];

  

        switch ([sender selectedSegmentIndex] ) {
            case 0:
                self.type = @"hot";
                [self.table setContentOffset:CGPointMake(0, -64) animated:YES];
                
                [self getData:_type];

                break;
            case 1:
                _type = @"recent";
                [self getData:_type];
                [self.table setContentOffset:CGPointMake(0,  -64) animated:YES];
                
                break;
            case 2:
           
                self.type = @"classic";
                [self getData:_type];
                [self.table setContentOffset:CGPointMake(0, -64) animated:YES];
                
                break;
            default:
                break;
        }
        
    
}
-(void)CreatTabbar
{
    self.downView = [[UIView alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 44, [UIScreen mainScreen].bounds.size.width, 44)];
    _downView.backgroundColor = [UIColor blackColor];
                           
    [self.view addSubview:_downView];
    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.backButton.frame =  CGRectMake(10, 10, 24, 24);
    
    [self.backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [self.backButton setImage:[UIImage imageNamed:@"iconfont-fanhui.png"] forState:UIControlStateNormal];
    [self.downView addSubview:_backButton];
    self.popButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.popButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 35, 10, 24, 24);
    
    [self.popButton addTarget:self action:@selector(popAction) forControlEvents:UIControlEventTouchUpInside];

    [self.popButton setImage:[UIImage imageNamed:@"iconfont-home.png"] forState:UIControlStateNormal];
    
    [self.downView addSubview:_popButton];
    
    UIView *black = [[UIView alloc]initWithFrame:CGRectMake(64 * 2, 568 - 64, 64, 64)];
    [self.view addSubview:black];
    black.layer.cornerRadius = 32;
    black.clipsToBounds = YES;
    
    black.backgroundColor = [UIColor blackColor];
    
    self.play = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.play.frame = CGRectMake(0, 0, 60, 60);
    self.play.center = black.center;
    
    [self.view addSubview:self.play];
    
    self.play.layer.cornerRadius = 30;
    [self.view bringSubviewToFront:self.play];
    self.play.clipsToBounds = YES;
    [self.play addTarget:self action:@selector(GotoPlay) forControlEvents:UIControlEventTouchUpInside];
    
    [self.play sd_setBackgroundImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.Playmodel.coverLarge]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"123.png"]];
    
    

}
-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
    
}
-(void)popAction
{
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _model.list.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ide = @"seg";

    SegTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ide];
    if (!cell) {
        cell = [[SegTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ide];
    }
    
    [cell setModel:_model num:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BoZhuViewController *vc = [[BoZhuViewController alloc]init];
    vc.MyId = [NSString stringWithFormat:@"%@",[[_model.list objectAtIndex:indexPath.row] objectForKey:@"id"] ];
    vc.title = [[_model.list objectAtIndex:indexPath.row] objectForKey:@"title"];
    
    [self.navigationController pushViewController:vc animated:YES];
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}


-(void)viewWillAppear:(BOOL)animated
{
    AudioStreamerManager *audioManager = [AudioStreamerManager shareInstanceManager];
    _Playmodel = audioManager.Mymodel;
    
    
    if (audioManager.flag) {
        
        if (!_Timer) {
            //            self.Timer = [NSTimer timerWithTimeInterval:0.01 target:self selector:@selector(zhuan) userInfo:nil repeats:YES];
            
            self.Timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(zhuan) userInfo:nil repeats:YES];
        }
        
        //将定时器加入循环
        //循环用于系统页面响应,定时器和进程
        //                [[NSRunLoop currentRunLoop] addTimer:_Timer forMode:NSDefaultRunLoopMode];
        
        
        [_Timer setFireDate:[NSDate distantPast]];
        
        
        
        
    }
    else
    {
        
        [_Timer setFireDate:[NSDate distantFuture]];
        
        //        if ([_Timer isValid]) {
        //            永久停止
        //            [self.Timer invalidate];
        //            _Timer =nil;
        //        }
        
        
    }
    
}
-(void)zhuan
{
    self.play.transform = CGAffineTransformRotate(self.play.transform,2 * M_PI / 60.0 /100);
    //    if (_model.coverLarge) {
    [self.play sd_setBackgroundImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.Playmodel.coverLarge]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"123.png"]];
    //    }
}

-(void)GotoPlay
{
    PlayViewController *vc = [[PlayViewController alloc]init];
    AudioStreamerManager *audioManager = [AudioStreamerManager shareInstanceManager];
    vc.model = audioManager.Mymodel;
    vc.DataSource = audioManager.Mydata;
    /**
     *  number不记录
     */
    
    //    vc.number = audioManager.num;
    vc.num = [NSString stringWithFormat:@"%ld",audioManager.num];
    
    
    [self presentViewController:vc animated:YES completion:nil];
    
}
-(void)viewDidDisappear:(BOOL)animated
{
    [self.Timer invalidate];
    _Timer =nil;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
